#include "manageform.h"
#include <QPainter>
#include "ntlauncher.h"
#include <QMessageBox>
#include <QDesktopWidget>
#include <QCloseEvent>
#include "CustomBox.h"
#include <QTime>
#include "funcmgr.h"

ManageForm::ManageForm(QWidget *parent) : QWidget(parent)
    , EventLoop(Q_NULLPTR)
    , NResult(NID_OK)
    , InitCode(QSNULL)
{
    ui.setupUi(this);

    setTabOrder(ui.Edit1, ui.Edit2);
    setTabOrder(ui.Edit2, ui.Edit3);
    setTabOrder(ui.Edit3, ui.Edit4);
    setTabOrder(ui.Edit4, ui.Edit5);

    DoTitleBar = new ManageFormTitle(this);
    DoTitleBar->move(0, 0);
    DoTitleBar->SetTitleWidth(this->width());
    QObject::connect(DoTitleBar, SIGNAL(NButtonCloseClicked()), this, SLOT(OnNCloseClicked()));

    //设置标题
    setWindowTitle(QStringLiteral("账号管理"));

    //无边框样式
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    //设置窗口背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    //信号
    QObject::connect(ui.FormYesButton, SIGNAL(clicked()), this, SLOT(OnYesClicked()));

    QObject::connect(ui.FormClsButton, SIGNAL(clicked()), this, SLOT(OnNCloseClicked()));

    //加载Radio
    NGroupFruits = new QButtonGroup(this);
    NGroupFruits->addButton(ui.RegRido, S_REG);
    NGroupFruits->addButton(ui.CharRido, S_STUCK);
    NGroupFruits->addButton(ui.PwRido, S_PASS);

    //Radio信号
    QObject::connect(ui.RegRido, SIGNAL(clicked()), this, SLOT(OnNRadioClickFruits()));
    QObject::connect(ui.CharRido, SIGNAL(clicked()), this, SLOT(OnNRadioClickFruits()));
    QObject::connect(ui.PwRido, SIGNAL(clicked()), this, SLOT(OnNRadioClickFruits()));
}

ManageForm::~ManageForm() { }

NChosseResult ManageForm::ShowForm()
{
    sFunc->FormInTopMainCenter(this);

    ui.Edit1->setFocus();
    SetRadioFruits(S_REG);

    //全局模态窗口
    setWindowModality(Qt::ApplicationModal);
    show();

    EventLoop = new QEventLoop(this);
    EventLoop->exec();

    return NResult;
}

void ManageForm::OnNCloseClicked()
{
    NResult = NID_OK;
    close();
}

void ManageForm::closeEvent(QCloseEvent *event)
{
    if (EventLoop != Q_NULLPTR)
        EventLoop->exit();

    event->accept();
}

int ManageForm::SetRandom(int Min, int Nax)
{
    static bool Status;
    if (!Status)
    {
        qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
        Status = true;
    }
    int Random = qrand() % (Nax - Min);
    Random = Min + Random;

    return Random;
}

void ManageForm::ShowWidget(StType Typ)
{
    switch (Typ)
    {
    case S_REG:
    {
        ui.TypeLabel->setText(QStringLiteral("账号注册"));
        ui.Tslabel->setText(QStringLiteral("*欢迎使用账号注册.请务必牢记账号密码"));

        ui.Lab2->setText(QStringLiteral("输入密码"));
        ui.Lab3->setText(QStringLiteral("确认密码"));
        ui.Cs1->setText(QStringLiteral("4-12位数字和字母"));
        ui.Cs2->setText(QStringLiteral("4-12位数字和字母"));
        ui.Cs3->setText(QStringLiteral("两次输入的密码一致"));

        ui.Lab4->setVisible(true);
        ui.Lab4->move(20, 260);
        ui.Edit4->setVisible(true);
        ui.Edit4->move(100, 260);
        ui.Cs4->setVisible(true);
        ui.Cs4->move(270, 260);
        ui.Cs4->setText(QStringLiteral("请填写邀请码"));

        ui.Lab5->setVisible(true);
        ui.Lab5->move(20, 300);
        ui.Edit5->setVisible(true);
        ui.Edit5->move(100, 300);
        ui.CsCode->setVisible(true);
        ui.CsCode->move(270, 300);
    }

    break;
    case S_STUCK:
    {
        ui.TypeLabel->setText(QStringLiteral("角色解卡"));
        ui.Tslabel->setText(QStringLiteral("*欢迎使用角色解卡 请确保角色处于离线"));

        ui.Lab2->setText(QStringLiteral("角色名字"));
        ui.Lab3->setText(QStringLiteral("输入密码"));
        ui.Cs2->setText(QStringLiteral("仔细输入角色名"));
        ui.Cs3->setText(QStringLiteral("必须正确输入密码"));

        ui.Lab4->setVisible(false);
        ui.Edit4->setVisible(false);
        ui.Cs4->setVisible(false);

        ui.Lab5->setVisible(true);
        ui.Lab5->move(20, 260);
        ui.Edit5->setVisible(true);
        ui.Edit5->move(100, 260);
        ui.CsCode->setVisible(true);
        ui.CsCode->move(270, 260);
    }
    break;
    case S_PASS:
    {
        ui.TypeLabel->setText(QStringLiteral("修改密码"));
        ui.Tslabel->setText(QStringLiteral("*欢迎使用修改密码.请务必牢记账号密码"));

        ui.Lab2->setText(QStringLiteral("旧的密码"));
        ui.Lab3->setText(QStringLiteral("新的密码"));
        ui.Cs1->setText(QStringLiteral("仔细输入账号"));
        ui.Cs2->setText(QStringLiteral("原来的密码"));
        ui.Cs3->setText(QStringLiteral("修改后的密码"));

        ui.Lab4->setVisible(false);
        ui.Edit4->setVisible(false);
        ui.Cs4->setVisible(false);

        ui.Lab5->setVisible(true);
        ui.Lab5->move(20, 260);
        ui.Edit5->setVisible(true);
        ui.Edit5->move(100, 260);
        ui.CsCode->setVisible(true);
        ui.CsCode->move(270, 260);
    }
    break;
    default:
        break;
    }
}

void ManageForm::SetRadioFruits(StType Typ)
{
    switch (Typ)
    {
    case S_REG:
    {
        ui.RegRido->click();
    }
    break;
    case S_STUCK:
    {
        ui.CharRido->click();
    }
    break;
    case S_PASS:
    {
        ui.PwRido->click();
    }
    break;
    }
}

//复选框Radio选中处理
void ManageForm::OnNRadioClickFruits()
{
    if (NGroupFruits->checkedId() < 0)
        return;

    ResetEditTextCode();

    ui.Cs5->setText(QSNULL);
    ui.Cs6->setText(QSNULL);

    ShowWidget(StType(NGroupFruits->checkedId()));
}

//判断是否是中文
bool ManageForm::IsCha(QString &QStr)
{
    int Count = QStr.count();
    bool Ret = true;
    for (int i = 0; i<Count; ++i)
    {
        QChar Cha = QStr.at(i);
        ushort Unicode = Cha.unicode();
        if ((Unicode >= 0x4E00 && Unicode <= 0x9FA5))
        {
            Ret = true;
        }
        else
        {
            Ret = false;
            break;
        }
    }
    return Ret;
}

//判断是否是英文和数字
bool ManageForm::IsEnglishOrNumber(QString &QStr)
{
    QByteArray ByteArray = QStr.toLatin1();
    const char *Data = ByteArray.data();
    bool Ret = true;
    while (*Data)
    {
        if ((*Data >= 'A' && *Data <= 'Z') || (*Data >= 'a' && *Data <= 'z') || (*Data >= '0' && *Data <= '9'))
        {
            Ret = true;
        }
        else
        {
            Ret = false;
            break;
        }
        Data++;
    }
    return Ret;
}

void ManageForm::OnYesClicked()
{
    NResult = NID_OK;

    QString Edit1 = ui.Edit1->text().trimmed();
    QString Edit2 = ui.Edit2->text().trimmed();
    QString Edit3 = ui.Edit3->text().trimmed();
    QString Edit4 = ui.Edit4->text().trimmed();
    QString Edit5 = ui.Edit5->text().trimmed();

    if (Edit1.contains(BanSplit, Qt::CaseInsensitive))
    {
        sFunc->MBoxShow(QStringLiteral("不能包含指定名称 %1\n\n请重新输入!").arg(BanSplit));
        return;
    }

    switch (StType(NGroupFruits->checkedId()))
    {
    case S_REG:
    {
        if (Edit1 == QSNULL || Edit2 == QSNULL || Edit3 == QSNULL || Edit4 == QSNULL || Edit5 == QSNULL)
        {
            sFunc->MBoxShow(QStringLiteral("输入的信息不完整!\n\n请重新输入!"));
            return;
        }

        if (Edit1.length() < 4 || Edit1.length() > 12)
        {
            sFunc->MBoxShow(QStringLiteral("账号太长或者太短!\n\n请重新输入!"));
            return;
        }
        if (!IsEnglishOrNumber(Edit1))
        {
            sFunc->MBoxShow(QStringLiteral("账号仅支持字母数字!\n\n请重新输入!"));
            return;
        }
        if (Edit2.length() < 4 || Edit2.length() > 12)
        {
            sFunc->MBoxShow(QStringLiteral("密码太长或者太短!\n\n请重新输入!"));
            return;
        }
        if (!IsEnglishOrNumber(Edit2))
        {
            sFunc->MBoxShow(QStringLiteral("密码仅支持字母数字!\n\n请重新输入!"));
            return;
        }
        if (Edit2 != Edit3)
        {
            sFunc->MBoxShow(QStringLiteral("两次输入的密码不一致!\n\n请重新输入!"));
            return;
        }
        if (Edit5 != InitCode)
        {
            sFunc->MBoxShow(QStringLiteral("验证码不正确!\n\n请重新输入!"));
            return;
        }

        QString SendMsg = QStringLiteral("%1").arg(QString::number(CmdReg) + Se + Edit1 + Se + Edit2 + Se + Edit4 + Se);
        emit EmitFuncSendTcpData(SendMsg);
    }
    break;
    case S_STUCK:
    {
        if (Edit1 == QSNULL || Edit2 == QSNULL || Edit3 == QSNULL || Edit5 == QSNULL)
        {
            sFunc->MBoxShow(QStringLiteral("输入的信息不完整!\n\n请重新输入!"));
            return;
        }

        if (Edit5 != InitCode)
        {
            sFunc->MBoxShow(QStringLiteral("验证码不正确!\n\n请重新输入!"));
            return;
        }
        QString SendMsg = QStringLiteral("%1").arg(QString::number(CmdPlayer) + Se + Edit1 + Se + Edit2 + Se + Edit3 + Se);
        emit EmitFuncSendTcpData(SendMsg);
    }
    break;
    case S_PASS:
    {
        if (Edit1 == QSNULL || Edit2 == QSNULL || Edit3 == QSNULL || Edit5 == QSNULL)
        {
            sFunc->MBoxShow(QStringLiteral("输入的信息不完整!\n\n请重新输入!"));
            return;
        }

        if (Edit2 == Edit3)
        {
            sFunc->MBoxShow(QStringLiteral("新的密码和旧的密码相同!\n\n请重新输入!"));
            return;
        }
        if (Edit5 != InitCode)
        {
            sFunc->MBoxShow(QStringLiteral("验证码不正确!\n\n请重新输入!"));
            return;
        }
        QString SendMsg = QStringLiteral("%1").arg(QString::number(CmdPass) + Se + Edit1 + Se + Edit2 + Se + Edit3 + Se);
        emit EmitFuncSendTcpData(SendMsg);
    }
    break;
    }
}

void ManageForm::SetTextComplete(QString Text1, QString Text2)
{
    ui.Cs5->setText(Text1);
    ui.Cs6->setText(Text2);
}

void ManageForm::ResetEditTextCode()
{
    ui.Edit1->setText(QSNULL);
    ui.Edit2->setText(QSNULL);
    ui.Edit3->setText(QSNULL);
    ui.Edit4->setText(QSNULL);
    ui.Edit5->setText(QSNULL);

    QString Code = QStringLiteral("%1").arg(QString::number(SetRandom(10001, 99999)));
    ui.CsCode->setText(Code);
    InitCode = Code;
}
